package loader;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import loader.parser.adapter.Adapter455Tuan;
import loader.parser.adapter.Adapter4MeiTuan;
import loader.parser.adapter.SiteAdapter;

public class Configuration {

	private static HashMap<String, SiteAdapter> adapters;
	private static String destDir;

	static {// read configuration file
		adapters = new HashMap<String, SiteAdapter>();
		try {
			readConfigFile();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private static void readConfigFile() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException {
		HashMap<String, String> names = new HashMap<String, String>();
		names.put("meituan", Adapter4MeiTuan.class.getName());
		names.put("wowotuan", Adapter455Tuan.class.getName());
		instantiateAdapters(names);
	}

	private static void instantiateAdapters(HashMap<String, String> names)
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		for (Entry<String, String> name : names.entrySet()) {
			SiteAdapter adapter = (SiteAdapter) Class.forName(name.getValue())
					.newInstance();
			adapters.put(name.getKey(), adapter);
		}
	}

	public static SiteAdapter getAdapter(String siteName) {
		return adapters.get(siteName);
	}

	public static String getDir() {
		return destDir;
	}

	public static Set<String> getSites() {
		return adapters.keySet();
	}
}
